package com.ljh.mall.product.web;

import com.ljh.mall.product.entity.CategoryEntity;
import com.ljh.mall.product.service.CategoryService;
import com.ljh.mall.product.vo.Catalog2VO;
import org.aspectj.lang.annotation.SuppressAjWarnings;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author ：刘剑鸿
 * @date ：Created in 2022/12/24 19:05
 * @description：
 * @modified By：
 * @version: $
 */
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;
    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    @GetMapping({"/","/home","/index"})
    public String index(Model model){
        // 查询出所有的一级分类的信息
        List<CategoryEntity> list = categoryService.getLeve1Category();
        model.addAttribute("categorys",list);
        // classPath:/templates/
        // .html
        return "index";
    }
    @ResponseBody
    @RequestMapping("/index/catalog.json")
    public Map<String, List<Catalog2VO>> getCatalog2JSON(){
        Map<String, List<Catalog2VO>> map = categoryService.getCatelog2JSON();
        return map;
    }
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){

        RLock myLock = redissonClient.getLock("myLock");
        //加锁
        myLock.lock();
        try{
            System.out.println("加锁成功...业务处理....." + Thread.currentThread().getName());
            Thread.sleep(30000);
        } catch (Exception e){

        } finally {
            System.out.println("释放锁成功..." +  Thread.currentThread().getName());
            //释放锁
            myLock.unlock();
        }
        return "hello";
    }
    @GetMapping("/writer")
    @ResponseBody
    public String writerValue(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        //
        RLock rLock = readWriteLock.writeLock();
        String s = null;
        rLock.lock(); // 加写锁
        try {
            System.out.println("加写锁成功....");
            s = UUID.randomUUID().toString();
            stringRedisTemplate.opsForValue().set("msg", s);
            Thread.sleep(30000);
        }catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return s;
    }

    @GetMapping("/reader")
    @ResponseBody
    public String readValue(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        //
        RLock rLock = readWriteLock.readLock();
        rLock.lock();
        String s = null;
        try {
            s = stringRedisTemplate.opsForValue().get("msg");
        } finally {
            rLock.unlock();
        }
        return s;
    }
    @GetMapping("/lockDoor")
    @ResponseBody
    public String lockDoor(){
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(3);
        try {
            door.await(); //等待数量降到0
        } catch (InterruptedException e){
            e.printStackTrace();
        }
        return "关门熄灯...";
    }

    @GetMapping("/goHome/{id}")
    @ResponseBody
    public String goHome(@PathVariable Long id){
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();
        return id + "下班走人";
    }

    @GetMapping("/park")
    @ResponseBody
    public String park(){
        RSemaphore park = redissonClient.getSemaphore("park");
        boolean b = true;
        try {
            b = park.tryAcquire();
        } catch (Exception e){
            e.printStackTrace();
        }
        return "停车是否成功:" + b;
    }

    @GetMapping("/release")
    @ResponseBody
    public String release(){
        RSemaphore park = redissonClient.getSemaphore("park");
        park.release();
        return "释放了一个车位";
    }
}
